package test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class settings {
	static String titleText = "";
	static String parity = "";
	static String role = "";
	static int i = 0;

	static List<WebElement> elements = new ArrayList<WebElement>();

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

		WebDriver driver = new ChromeDriver();

		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

		driver.get("http://qa:bfh365Dkjfh@qa.taxi.dancosoft.com/site/login");

		Thread.sleep(3000);

		driver.findElement(By.cssSelector(".js-login")).sendKeys("autotest");

		driver.findElement(By.name("password")).sendKeys("autotest");

		driver.findElement(By.cssSelector("button")).click();
		Thread.sleep(2000);

		driver.get("http://qa.taxi.dancosoft.com/site/employeeCard");

		Thread.sleep(2000);

		WebElement element = driver.findElement(By.cssSelector("[class='js-typeahead']"));
		element.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN);
		Thread.sleep(300);
		element.sendKeys(Keys.ENTER);

		Thread.sleep(500);

		element = driver.findElement(By.cssSelector("[class='js-typeahead']"));
		element.sendKeys(Keys.ARROW_DOWN);
		Thread.sleep(300);
		element.sendKeys(Keys.ENTER);

//		element.sendKeys(Keys.TAB, Keys.TAB, Keys.TAB, Keys.TAB);
		Thread.sleep(3000);

		new Actions(driver)
				.moveToElement(
						driver.findElement(By
								.xpath("/html/body/div[@id='loaded']/div[@class='container taxi-container js-app-root']/div[@class='row taxi-row'][1]/div[@class='span5']/div[@class='row taxi-row']/div[@class='span12']/div[@class='taxi-grid-wrap']/*/div[@class='slick-viewport']/div[@class='grid-canvas']/div[@class='slick-grid-content slick-row even']/div[@class='slick-cell l2 r2']")))
				.click().perform();

		// driver.findElement(
		// By.xpath("/html/body/div[@id='loaded']/div[@class='container taxi-container js-app-root']/div[@class='row taxi-row'][1]/div[@class='span5']/div[@class='row taxi-row']/div[@class='span12']/div[@class='taxi-grid-wrap']/*/div[@class='slick-viewport']/div[@class='grid-canvas']/div[@class='slick-grid-content slick-row even']/div[@class='slick-cell l2 r2']"))
		// .click();
		Thread.sleep(300);
		// WebElement Table = driver
		// .findElement(By
		// .xpath("/html/body/div[@id='loaded']/div[@class='container taxi-container js-app-root']/div[@class='row taxi-row'][1]/div[@class='span5']/div[@class='row taxi-row']/div[@class='span12']/div[@class='taxi-grid-wrap']/*/div[@class='slick-viewport']/div[@class='grid-canvas']"));
		// Table.sendKeys(Keys.ARROW_DOWN);

		// firstInTable.click();
		// Thread.sleep(300);
		// firstInTable.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN,
		// Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN,
		// Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN);

		// JavascriptExecutor jse = (JavascriptExecutor) driver;
		// jse.executeScript("window.scrollBy(0,-250)", "");
		Thread.sleep(1500);

		try {
			for (int i = 0; i <= 35; i++) {
				elements = driver.findElements(By.cssSelector("[class='grid-canvas']"));

				new Actions(driver)
						.moveToElement(
								driver.findElement(By
										.xpath("/html/body/div[@id='loaded']/div[@class='container taxi-container js-app-root']/div[@class='row taxi-row'][1]/div[@class='span5']/div[@class='row taxi-row']/div[@class='span12']/div[@class='taxi-grid-wrap']/*/div[@class='slick-viewport']/div[@class='grid-canvas']")))
						.sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN,
								Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ARROW_DOWN).perform();
			}

			checkRole(elements, "Диспетчер");

		} catch (Exception e) {
			System.out.println("Не нашёл");
		}

		Thread.sleep(1000);

		driver.quit();
	}

	private static boolean checkRole(List<WebElement> elements, String role) {

		for (WebElement element : elements) {
			if (element.getText().contains(role)) {
				System.out.println("Cотрудник с ролью " + role + " найден");
				return true;
			}
		}

		System.out.println("В списке нет сотрудников с ролью " + role);
		for (WebElement element : elements) {
			System.out.println(element.getText());
		}

		return false;
	}
}
